using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Web.Administration;
using Microsoft.Web.Management.Client;
using Microsoft.Web.Management.Client.Win32;
using Microsoft.Web.Management.Server;

namespace Microsoft.Web.Management.Iis.Client
{
    /// <summary>
    /// Provides user interface for Site logging
    /// </summary>
    [ModulePageIdentifier("50A59EF1-51E1-4277-8497-D26C91DC90F9")]
    internal sealed class CentralLoggingPage : ModuleDialogPage, IModuleChildPage
    {
        /************************************************************************
           FIELDS
         ************************************************************************/
        PropertyBag                 _properties     = new PropertyBag();
        LoggingModuleServiceProxy   _moduleProxy;
        TaskList                    _taskList;
        Boolean                     _hasChanges;
#pragma warning disable 649
        Boolean                     _isReadOnly;
#pragma warning restore 649

        Boolean                     _enabled;
        LoggingRolloverPeriod       _period             = LoggingRolloverPeriod.Daily;
        String                      _directory;
        Int32                       _truncateSize;
        Boolean                     _localTimeRollover;
        LogFormat                   _logFileFormat      = LogFormat.W3c;
        LogExtFileFlags             _logExtFileFlags;
        LoggingMode                 _loggingMode;

        /************************************************************************
           CONSTRUCTORS
         ************************************************************************/
        /// <summary>
        /// Creates an instance of a CentralLoggingPage object.
        /// </summary>
        public CentralLoggingPage()
        {
            InitializeComponent();
        }

        /************************************************************************
           PROPERTIES
         ************************************************************************/
        /// <summary>
        /// Gets a value indicating whether the page can apply pages.
        /// </summary>
        protected override bool CanApplyChanges
        {
            get { return HasChanges; }
        }
        /// <summary>
        /// Gets a value indicating whether the page can refresh.
        /// </summary>
        /// <remarks>
        /// Instances of CentralLoggingPage always return true.
        /// </remarks>
        protected override bool CanRefresh
        {
            get { return true; }
        }
        /// <summary>
        /// Gets a value indicating whether the page has changes.
        /// </summary>
        protected override bool HasChanges
        {
            get { return _hasChanges; }
        }

        /// <summary>
        /// Gets or sets a reference to the parent of the current module page.
        /// </summary>
        public IModulePage ParentPage { get; set; }

        /// <summary>
        /// Gets or sets a value that determines whether the page is read only.
        /// </summary>
        protected override bool ReadOnly
        {
            get { return _isReadOnly; }
        }
        /// <summary>
        /// Gets a TaskListCollection that contains tasks available for the current page.
        /// </summary>
        protected override TaskListCollection Tasks
        {
            get
            {
                if (_taskList == null)
                {
                    _taskList = new PageTaskList(this);
                }
                TaskListCollection col = base.Tasks;
                col.Add(_taskList);
                return col;
            }
        }


        /************************************************************************
           METHODS                                                              
         ************************************************************************/
        /// <summary>
        /// Executed when you click the apply button on the task list
        /// </summary>
        /// <returns>true if settings are applied property, otherwise false</returns>
        protected override bool ApplyChanges()
        {
            Boolean temp;

            GetSettingsFromUi();
            
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                if (_loggingMode == LoggingMode.Site)
                {
                    _moduleProxy.UpdateSiteSettings(
                        (PropertyBag)_properties[LoggingPropertyIndexes.SITE_DEFAULTS_ELEMENT_INDEX]);
                }
                else if(_loggingMode == LoggingMode.Binary )
                {
                    _moduleProxy.UpdateCentralBinaryLogSettings(
                        (PropertyBag)_properties[LoggingPropertyIndexes.BINARY_ELEMENT_INDEX]);
                }
                else if (_loggingMode == LoggingMode.W3C)
                {
                    _moduleProxy.UpdateCentralW3CLogSettings(
                        (PropertyBag)_properties[LoggingPropertyIndexes.W3C_ELEMENT_INDEX]);
                }
                temp = true;
            }
            catch (Exception ex)
            {
                DisplayErrorMessage(ex, Resources.ResourceManager);
                temp = false;
            }
            finally
            {
                Cursor.Current = Cursors.Default;
                _hasChanges = false;
                UpdateState(false);
                Update();
            }
            return temp;
        }

        /// <summary>
        /// Begins a request to get properties from the module service proxy.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BeginGetProperties(object sender, DoWorkEventArgs e)
        {
            e.Result = _moduleProxy.GetConfigurationSettings();
        }

        /// <summary>
        /// Cancels changes and resets the user interface to original values
        /// </summary>
        protected override void CancelChanges()
        {
            InitializeUi();
            _hasChanges = false;
            Update();
        }

        /// <summary>
        /// Ends a request to get properties from the module service proxy.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EndGetProperties(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                _properties = e.Result as PropertyBag;
                if (_properties != null)
                {

                    PropertyBag uiProperties = null;
                    switch (_loggingMode)
                    {
                        case LoggingMode.Site:
                            uiProperties = (PropertyBag)_properties[LoggingPropertyIndexes.SITE_DEFAULTS_ELEMENT_INDEX];
                            _logExtFileFlags = (LogExtFileFlags)uiProperties[LoggingPropertyIndexes.LOG_EXT_FILE_FLAGS_INDEX];
                            _logFileFormat = (LogFormat)uiProperties[LoggingPropertyIndexes.LOG_FILE_FORMAT_INDEX];
                            break;
                        case LoggingMode.Binary:
                            uiProperties = (PropertyBag)_properties[LoggingPropertyIndexes.BINARY_ELEMENT_INDEX];
                            break;
                        case LoggingMode.W3C:
                            uiProperties = (PropertyBag)_properties[LoggingPropertyIndexes.W3C_ELEMENT_INDEX];
                            break;
                    }

                    _enabled = (Boolean)uiProperties[LoggingPropertyIndexes.ENABLED_INDEX];
                    _period = (LoggingRolloverPeriod)uiProperties[LoggingPropertyIndexes.PERIOD_INDEX];
                    _directory = (String)uiProperties[LoggingPropertyIndexes.DIRECTORY_INDEX];
                    var size = (Int32)(((Int64)uiProperties[LoggingPropertyIndexes.TRUNCATE_SIZE_INDEX]) / 1024 / 1024);
                    _truncateSize = size;
                    _localTimeRollover = (Boolean)uiProperties[LoggingPropertyIndexes.LOCAL_TIME_ROLLOVER_INDEX];

                    InitializeUi();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
                // TODO: Handle exception case.
                // BUG: Handle exception case.
            }
        }

        /// <summary>
        /// Performs an asynchronous request for the logging settings.
        /// </summary>
        private void GetSettings()
        {
            StartAsyncTask(Resources.LoggingGettingSettings,
                BeginGetProperties,
                EndGetProperties);
            _hasChanges = false;
            Update();
        }

        /// <summary>
        /// Gets settings from the user interface.
        /// </summary>
        /// <remarks>
        /// This is executed before applying changes.
        /// </remarks>
        private void GetSettingsFromUi()
        {
            // Update all settings based on the user interface.
            //
            PropertyBag uiProperties = null;

            _enabled = _enableLoggingCheckBox.Checked;
            _directory = _logFileDirectoryTextBox.Text;
            if (_dailyRadioButton.Checked)
            {
                _period = LoggingRolloverPeriod.Daily;
            }
            else if (_hourlyRadioButton.Checked)
            {
                _period = LoggingRolloverPeriod.Hourly;
            }
            else if (_fileSizeRadioButton.Checked)
            {
                _period = LoggingRolloverPeriod.MaxSize;
            }
            else if (_monthlyRadioButton.Checked)
            {
                _period = LoggingRolloverPeriod.Monthly;
            }
            else if (_weeklyRadioButton.Checked)
            {
                _period = LoggingRolloverPeriod.Weekly;
            }
            _localTimeRollover = _rolloverCheckBox.Checked;
            _truncateSize = (Int32)_fileSizeNumericUpDown.Value;


            // Determine which property bag to update
            if (_loggingMode == LoggingMode.Site)
            {
                uiProperties = (PropertyBag)_properties[LoggingPropertyIndexes.SITE_DEFAULTS_ELEMENT_INDEX];
                switch (_logFileFormatComboBox.SelectedIndex)
                {
                    case 0:
                        _logFileFormat = LogFormat.Iis;
                        break;
                    case 1:
                        _logFileFormat = LogFormat.Ncsa;
                        break;
                    case 2:
                        _logFileFormat = LogFormat.W3c;
                        break;
                    case 3:
                        _logFileFormat = LogFormat.Custom;
                        break;
                }
                uiProperties[LoggingPropertyIndexes.LOG_EXT_FILE_FLAGS_INDEX] = (Int32)_logExtFileFlags;
                uiProperties[LoggingPropertyIndexes.LOG_FILE_FORMAT_INDEX] = (Int32)_logFileFormat;
            }
            else if (_loggingMode == LoggingMode.Binary)
            {
                uiProperties = (PropertyBag)_properties[LoggingPropertyIndexes.BINARY_ELEMENT_INDEX];
            }
            else if (_loggingMode == LoggingMode.W3C)
            {
                uiProperties = (PropertyBag)_properties[LoggingPropertyIndexes.W3C_ELEMENT_INDEX];
            }

            // Update the loggingmode-specific property bag
            uiProperties[LoggingPropertyIndexes.ENABLED_INDEX] = _enabled;
            uiProperties[LoggingPropertyIndexes.PERIOD_INDEX] = (Int32)_period;
            uiProperties[LoggingPropertyIndexes.DIRECTORY_INDEX] = _directory;
            Int64 size = _truncateSize * 1024 * 1024;
            uiProperties[LoggingPropertyIndexes.TRUNCATE_SIZE_INDEX] = size;
            uiProperties[LoggingPropertyIndexes.LOCAL_TIME_ROLLOVER_INDEX] = _localTimeRollover;


            // Update the main property bag
            if (_loggingMode == LoggingMode.Site)
            {
                _properties[LoggingPropertyIndexes.SITE_DEFAULTS_ELEMENT_INDEX] = uiProperties;
            }
            else if (_loggingMode == LoggingMode.Binary)
            {
                _properties[LoggingPropertyIndexes.BINARY_ELEMENT_INDEX] = uiProperties;
            }
            else if (_loggingMode == LoggingMode.W3C)
            {
                _properties[LoggingPropertyIndexes.W3C_ELEMENT_INDEX] = uiProperties;
            }
        }

        /// <summary>
        /// Executed when the page is navigated to from another page.
        /// </summary>
        /// <param name="navigationData"></param>
        protected override void Initialize(object navigationData)
        {
            base.Initialize(navigationData);
            if ((navigationData != null) && (navigationData is PropertyBag))
            {
                var data = (PropertyBag)navigationData;
                _loggingMode = ((LoggingMode)data[0]);
                if ( _loggingMode== LoggingMode.Site )
                {
                    _logFileFormatLabel.Visible = true;
                    _logFileFormatComboBox.Visible = true;
                }
                else if ( _loggingMode == LoggingMode.Binary ) 
                {
                    _logFileFormatLabel.Visible = false;
                    _logFileFormatComboBox.Visible = false;
                }
                else if ( _loggingMode == LoggingMode.W3C )
                {
                    _logFileFormatLabel.Visible = false;
                    _logFileFormatComboBox.Visible = false;
                }
            }
        }

        /// <summary>
        /// Initializes the user interface.
        /// </summary>
        private void InitializeUi()
        {
            _fileSizeNumericUpDown.Enabled = false;

            _enableLoggingCheckBox.Checked = _enabled;
            _logFileDirectoryTextBox.Text = _directory;
            switch (_period)
            {
                case LoggingRolloverPeriod.Daily:
                    _dailyRadioButton.Checked = true;
                    break;
                case LoggingRolloverPeriod.Hourly:
                    _hourlyRadioButton.Checked = true;
                    break;
                case LoggingRolloverPeriod.MaxSize:
                    _fileSizeRadioButton.Checked = true;
                    _fileSizeNumericUpDown.Enabled = true;
                    break;
                case LoggingRolloverPeriod.Monthly:
                    _monthlyRadioButton.Checked = true;
                    break;
                case LoggingRolloverPeriod.Weekly:
                    _weeklyRadioButton.Checked = true;
                    break;
            }
            _rolloverCheckBox.Checked = _localTimeRollover;
            _fileSizeNumericUpDown.Value = _truncateSize;
            switch (_logFileFormat)
            {
                case LogFormat.Custom:
                    _logFileFormatComboBox.SelectedIndex = 3;
                    break;
                case LogFormat.Iis:
                    _logFileFormatComboBox.SelectedIndex = 0;
                    break;
                case LogFormat.Ncsa:
                    _logFileFormatComboBox.SelectedIndex = 1;
                    break;
                case LogFormat.W3c:
                    _logFileFormatComboBox.SelectedIndex = 2;
                    break;
            }
            switch (_loggingMode)
            {
                case LoggingMode.Site:
                    _siteLoggingSettingsGroupBox.Text = Resources.ConfigureSiteDefaults;
                    break;
                case LoggingMode.Binary:
                    _siteLoggingSettingsGroupBox.Text = Resources.ConfigureCentralBinary;
                    break;
                case LoggingMode.W3C:
                    _siteLoggingSettingsGroupBox.Text = Resources.ConfigureCentralW3C;
                    break;
            }
            _hasChanges = false;
            SetToolTips();
            Update();
        }

        /// <summary>
        /// Executed when the module is activated
        /// </summary>
        /// <param name="initialActivation">true if this is the first activation, otherwise false.</param>
        protected override void OnActivated(bool initialActivation)
        {
            base.OnActivated(initialActivation);
            _moduleProxy = (LoggingModuleServiceProxy)CreateProxy(typeof(LoggingModuleServiceProxy));
            GetSettings();
        }
        
        /// <summary>
        /// Sets tooltips on all of the user interface elements.
        /// </summary>
        private void SetToolTips()
        {
            String toUse;
            switch (_loggingMode)
            {
                case LoggingMode.Site:
                    toUse = "<system.applicationHost>\r\n  <sites>\r\n    <siteDefaults>\r\n      <logFile {0}=\"{1}\"/>\r\n    </siteDefaults>\r\n  </sites>\r\n</system.applicationHost>";
                    break;
                case LoggingMode.Binary:
                    toUse = "<system.applicationHost>\r\n  <log centralLogFileMode=\"CentralBinary\">\r\n    <centralBinaryLogFile {0}=\"{1}\"/>\r\n  </log>\r\n</system.applicationHost>";
                    break;
                case LoggingMode.W3C:
                    toUse = "<system.applicationHost>\r\n  <log centralLogFileMode=\"CentralW3C\">\r\n    <centralW3CLogFile {0}=\"{1}\"/>\r\n  </log>\r\n</system.applicationHost>";
                    break;
                default:
                    return;
            }
            // Enabled checkbox
            _toolTip1.SetToolTip(_enableLoggingCheckBox,
                String.Format(toUse, "enabled",
                    _enableLoggingCheckBox.Checked));
            // Directory textbox
            _toolTip1.SetToolTip(_logFileDirectoryTextBox,
                String.Format(toUse, "directory",
                    _logFileDirectoryTextBox.Text));
            // File Size textbox
            _toolTip1.SetToolTip(_fileSizeNumericUpDown,
                String.Format(toUse, "truncateSize",
                    _fileSizeNumericUpDown.Value * 1024 * 1024));
            // Daily Radio Button
            _toolTip1.SetToolTip(_dailyRadioButton,
                String.Format(toUse, "period",
                    LoggingRolloverPeriod.Daily));
            // Hourly Radio Button
            _toolTip1.SetToolTip(_hourlyRadioButton,
                String.Format(toUse, "period",
                    LoggingRolloverPeriod.Hourly));
            // File Size Radio Button
            _toolTip1.SetToolTip(_fileSizeRadioButton,
                String.Format(toUse, "period",
                    LoggingRolloverPeriod.MaxSize));
            // File Size Radio Button
            _toolTip1.SetToolTip(_monthlyRadioButton,
                String.Format(toUse, "period",
                    LoggingRolloverPeriod.Monthly));
            // Weekly Radio Button
            _toolTip1.SetToolTip(_weeklyRadioButton,
                String.Format(toUse, "period",
                    LoggingRolloverPeriod.Weekly));
            // Rollover checkbox
            _toolTip1.SetToolTip(_rolloverCheckBox,
                String.Format(toUse,"localTimeRollover",
                    _rolloverCheckBox.Checked));
            // Log Format Combobox
            string temp = "";
            switch (_logFileFormatComboBox.SelectedIndex)
            {
                case 3:
                    temp = LogFormat.Custom.ToString();
                    break;
                case 0:
                    temp = LogFormat.Iis.ToString();
                    break;
                case 1:
                    temp = LogFormat.Ncsa.ToString();
                    break;
                case 2:
                    temp = LogFormat.W3c.ToString();
                    break;
            }
            // ReSharper disable AssignNullToNotNullAttribute
            _toolTip1.SetToolTip(_logFileFormatComboBox,
                String.Format(toUse, "logFormat",
                temp));
            // ReSharper restore AssignNullToNotNullAttribute

        }
        /// <summary>
        /// Creates and displays an instance of a dialog box that can configure logging file flags.
        /// </summary>
        public void ShowSiteFlags()
        {
            var dlg = new LogFileFlagsDialog
                          {
                              LoggingMode = LoggingMode.Site,
                              IsServerContext = false,
                              LogExtFileFlags = _logExtFileFlags,
                              StartPosition = FormStartPosition.CenterParent
                          };
            DialogResult dr = dlg.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                _logExtFileFlags = dlg.LogExtFileFlags;
                UpdateState(true);
            }
        }
        /// <summary>
        /// Updates the dirty data state of the page and updates the tooltips
        /// </summary>
        /// <param name="isDirty"></param>
        private void UpdateState(bool isDirty)
        {
            _hasChanges = (_hasChanges || isDirty);
            SetToolTips();
            Update();
        }

        #region Control initialization and layout

        private GroupBox _siteLoggingSettingsGroupBox;
        private FolderBrowserDialog _folderBrowserDialog1;
        private CheckBox _enableLoggingCheckBox;
        private Panel _pnlLogFileFormat;
        private CheckBox _rolloverCheckBox;
        private Label _logFileDirectoryLabel;
        private Button _browseDirectoryButton;
        private TextBox _logFileDirectoryTextBox;
        private GroupBox _logScheduleGroupBox;
        private Label _megabytesLabel;
        private NumericUpDown _fileSizeNumericUpDown;
        private RadioButton _fileSizeRadioButton;
        private RadioButton _monthlyRadioButton;
        private RadioButton _weeklyRadioButton;
        private RadioButton _dailyRadioButton;
        private RadioButton _hourlyRadioButton;
        private Label _logFileFormatLabel;
        private ComboBox _logFileFormatComboBox;
        private ToolTip _toolTip1;

        private void InitializeComponent()
        {
            _siteLoggingSettingsGroupBox = new GroupBox();
            _pnlLogFileFormat = new Panel();
            _rolloverCheckBox = new CheckBox();
            _logFileDirectoryLabel = new Label();
            _browseDirectoryButton = new Button();
            _logFileDirectoryTextBox = new TextBox();
            _logScheduleGroupBox = new GroupBox();
            _megabytesLabel = new Label();
            _fileSizeNumericUpDown = new NumericUpDown();
            _fileSizeRadioButton = new RadioButton();
            _monthlyRadioButton = new RadioButton();
            _weeklyRadioButton = new RadioButton();
            _dailyRadioButton = new RadioButton();
            _hourlyRadioButton = new RadioButton();
            _logFileFormatLabel = new Label();
            _logFileFormatComboBox = new ComboBox();
            _enableLoggingCheckBox = new CheckBox();
            _folderBrowserDialog1 = new FolderBrowserDialog();
            _toolTip1 = new ToolTip();
            _siteLoggingSettingsGroupBox.SuspendLayout();
            _pnlLogFileFormat.SuspendLayout();
            _logScheduleGroupBox.SuspendLayout();
            ((ISupportInitialize)(_fileSizeNumericUpDown)).BeginInit();
            SuspendLayout();
            // 
            // siteLoggingSettingsGroupBox
            // 
            _siteLoggingSettingsGroupBox.Controls.Add(_pnlLogFileFormat);
            _siteLoggingSettingsGroupBox.Controls.Add(_enableLoggingCheckBox);
            _siteLoggingSettingsGroupBox.Location = new System.Drawing.Point(0, 0);
            _siteLoggingSettingsGroupBox.Name = "_siteLoggingSettingsGroupBox";
            _siteLoggingSettingsGroupBox.Size = new System.Drawing.Size(349, 333);
            _siteLoggingSettingsGroupBox.TabIndex = 1;
            _siteLoggingSettingsGroupBox.TabStop = false;
            _siteLoggingSettingsGroupBox.Text = Resources.LoggingUiPageSiteLoggingSettingsGroupBox;
            // 
            // pnlLogFileFormat
            // 
            _pnlLogFileFormat.Controls.Add(_rolloverCheckBox);
            _pnlLogFileFormat.Controls.Add(_logFileDirectoryLabel);
            _pnlLogFileFormat.Controls.Add(_browseDirectoryButton);
            _pnlLogFileFormat.Controls.Add(_logFileDirectoryTextBox);
            _pnlLogFileFormat.Controls.Add(_logScheduleGroupBox);
            _pnlLogFileFormat.Controls.Add(_logFileFormatLabel);
            _pnlLogFileFormat.Controls.Add(_logFileFormatComboBox);
            _pnlLogFileFormat.Enabled = false;
            _pnlLogFileFormat.Location = new System.Drawing.Point(24, 44);
            _pnlLogFileFormat.Name = "_pnlLogFileFormat";
            _pnlLogFileFormat.Size = new System.Drawing.Size(317, 281);
            _pnlLogFileFormat.TabIndex = 1;
            // 
            // rolloverCheckBox
            // 
            _rolloverCheckBox.AutoSize = true;
            _rolloverCheckBox.Location = new System.Drawing.Point(10, 217);
            _rolloverCheckBox.Name = "_rolloverCheckBox";
            _rolloverCheckBox.Size = new System.Drawing.Size(218, 17);
            _rolloverCheckBox.TabIndex = 15;
            _rolloverCheckBox.Text = Resources.LoggingUiPageRolloverCheckBox;
            _rolloverCheckBox.UseVisualStyleBackColor = true;
            _rolloverCheckBox.CheckedChanged += OnRolloverCheckBoxCheckedChanged;
            // 
            // logFileDirectoryLabel
            // 
            _logFileDirectoryLabel.AutoSize = true;
            _logFileDirectoryLabel.Location = new System.Drawing.Point(7, 237);
            _logFileDirectoryLabel.Name = "_logFileDirectoryLabel";
            _logFileDirectoryLabel.Size = new System.Drawing.Size(87, 13);
            _logFileDirectoryLabel.TabIndex = 16;
            _logFileDirectoryLabel.Text = Resources.LoggingUiPageLogFileDirectoryLabel;
            // 
            // browseDirectoryButton
            // 
            _browseDirectoryButton.Location = new System.Drawing.Point(245, 251);
            _browseDirectoryButton.Name = "_browseDirectoryButton";
            _browseDirectoryButton.Size = new System.Drawing.Size(63, 23);
            _browseDirectoryButton.TabIndex = 18;
            _browseDirectoryButton.Text = Resources.LoggingUiPageBrowseDirectoryButton;
            _browseDirectoryButton.UseVisualStyleBackColor = true;
            _browseDirectoryButton.Click += OnBrowseDirectoryButtonClick;
            // 
            // logFileDirectoryTextBox
            // 
            _logFileDirectoryTextBox.Location = new System.Drawing.Point(9, 253);
            _logFileDirectoryTextBox.Name = "_logFileDirectoryTextBox";
            _logFileDirectoryTextBox.Size = new System.Drawing.Size(230, 20);
            _logFileDirectoryTextBox.TabIndex = 17;
            _logFileDirectoryTextBox.TextChanged += OnLogFileDirectoryTextBoxTextChanged;
            // 
            // logScheduleGroupBox
            // 
            _logScheduleGroupBox.Controls.Add(_megabytesLabel);
            _logScheduleGroupBox.Controls.Add(_fileSizeNumericUpDown);
            _logScheduleGroupBox.Controls.Add(_fileSizeRadioButton);
            _logScheduleGroupBox.Controls.Add(_monthlyRadioButton);
            _logScheduleGroupBox.Controls.Add(_weeklyRadioButton);
            _logScheduleGroupBox.Controls.Add(_dailyRadioButton);
            _logScheduleGroupBox.Controls.Add(_hourlyRadioButton);
            _logScheduleGroupBox.Location = new System.Drawing.Point(9, 45);
            _logScheduleGroupBox.Name = "_logScheduleGroupBox";
            _logScheduleGroupBox.Size = new System.Drawing.Size(299, 166);
            _logScheduleGroupBox.TabIndex = 14;
            _logScheduleGroupBox.TabStop = false;
            _logScheduleGroupBox.Text = Resources.LoggingUiPageLogScheduleGroupBox;
            // 
            // megabytesLabel
            // 
            _megabytesLabel.AutoSize = true;
            _megabytesLabel.Enabled = false;
            _megabytesLabel.Location = new System.Drawing.Point(100, 141);
            _megabytesLabel.Name = "_megabytesLabel";
            _megabytesLabel.Size = new System.Drawing.Size(23, 13);
            _megabytesLabel.TabIndex = 6;
            _megabytesLabel.Text = Resources.LoggingUiPageMegabytesLabel;
            // 
            // fileSizeNumericUpDown
            // 
            _fileSizeNumericUpDown.Enabled = false;
            _fileSizeNumericUpDown.Location = new System.Drawing.Point(32, 139);
            _fileSizeNumericUpDown.Name = "_fileSizeNumericUpDown";
            _fileSizeNumericUpDown.Size = new System.Drawing.Size(62, 20);
            _fileSizeNumericUpDown.TabIndex = 5;
            _fileSizeNumericUpDown.VisibleChanged += OnFileSizeNumericUpDownVisibleChanged;
            // 
            // fileSizeRadioButton
            // 
            _fileSizeRadioButton.AutoSize = true;
            _fileSizeRadioButton.Location = new System.Drawing.Point(7, 116);
            _fileSizeRadioButton.Name = "_fileSizeRadioButton";
            _fileSizeRadioButton.Size = new System.Drawing.Size(135, 17);
            _fileSizeRadioButton.TabIndex = 4;
            _fileSizeRadioButton.TabStop = true;
            _fileSizeRadioButton.Text = Resources.LoggingUiPageFileSizeRadioButton;
            _fileSizeRadioButton.UseVisualStyleBackColor = true;
            _fileSizeRadioButton.CheckedChanged += OnFileSizeRadioButtonCheckedChanged;
            // 
            // monthlyRadioButton
            // 
            _monthlyRadioButton.AutoSize = true;
            _monthlyRadioButton.Location = new System.Drawing.Point(7, 92);
            _monthlyRadioButton.Name = "_monthlyRadioButton";
            _monthlyRadioButton.Size = new System.Drawing.Size(62, 17);
            _monthlyRadioButton.TabIndex = 3;
            _monthlyRadioButton.TabStop = true;
            _monthlyRadioButton.Text = Resources.LoggingUiPageMonthlyRadioButton;
            _monthlyRadioButton.UseVisualStyleBackColor = true;
            _monthlyRadioButton.CheckedChanged += OnMonthlyRadioButtonCheckedChanged;
            // 
            // weeklyRadioButton
            // 
            _weeklyRadioButton.AutoSize = true;
            _weeklyRadioButton.Location = new System.Drawing.Point(7, 68);
            _weeklyRadioButton.Name = "_weeklyRadioButton";
            _weeklyRadioButton.Size = new System.Drawing.Size(61, 17);
            _weeklyRadioButton.TabIndex = 2;
            _weeklyRadioButton.TabStop = true;
            _weeklyRadioButton.Text = Resources.LoggingUiPageWeeklyRadioButton;
            _weeklyRadioButton.UseVisualStyleBackColor = true;
            _weeklyRadioButton.CheckedChanged += OnWeeklyRadioButtonCheckedChanged;
            // 
            // dailyRadioButton
            // 
            _dailyRadioButton.AutoSize = true;
            _dailyRadioButton.Location = new System.Drawing.Point(7, 44);
            _dailyRadioButton.Name = "_dailyRadioButton";
            _dailyRadioButton.Size = new System.Drawing.Size(48, 17);
            _dailyRadioButton.TabIndex = 1;
            _dailyRadioButton.TabStop = true;
            _dailyRadioButton.Text = Resources.LoggingUiPageDailyRadioButton;
            _dailyRadioButton.UseVisualStyleBackColor = true;
            _dailyRadioButton.CheckedChanged += OnDailyRadioButtonCheckedChanged;
            // 
            // hourlyRadioButton
            // 
            _hourlyRadioButton.AutoSize = true;
            _hourlyRadioButton.Location = new System.Drawing.Point(7, 20);
            _hourlyRadioButton.Name = "_hourlyRadioButton";
            _hourlyRadioButton.Size = new System.Drawing.Size(55, 17);
            _hourlyRadioButton.TabIndex = 0;
            _hourlyRadioButton.TabStop = true;
            _hourlyRadioButton.Text = Resources.LoggingUiPageHourlyRadioButton;
            _hourlyRadioButton.UseVisualStyleBackColor = true;
            _hourlyRadioButton.CheckedChanged += OnHourlyRadioButtonCheckedChanged;
            // 
            // logFileFormatLabel
            // 
            _logFileFormatLabel.AutoSize = true;
            _logFileFormatLabel.Location = new System.Drawing.Point(7, 0);
            _logFileFormatLabel.Name = "_logFileFormatLabel";
            _logFileFormatLabel.Size = new System.Drawing.Size(76, 13);
            _logFileFormatLabel.TabIndex = 11;
            _logFileFormatLabel.Text = Resources.LoggingUiPageLogFileFormatLabel;
            // 
            // logFileFormatComboBox
            // 
            _logFileFormatComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            _logFileFormatComboBox.FormattingEnabled = true;
            _logFileFormatComboBox.Items.AddRange(new object[] {
            Resources.LoggingUiPageLogFileFormatComboBoxIis,
            Resources.LoggingUiPageLogFileFormatComboBoxNCSA,
            Resources.LoggingUiPageLogFileFormatCombBoxW3c,
            Resources.LoggingUiPageLogFileFormatComboBoxCustom});
            _logFileFormatComboBox.Location = new System.Drawing.Point(9, 16);
            _logFileFormatComboBox.Name = "_logFileFormatComboBox";
            _logFileFormatComboBox.Size = new System.Drawing.Size(299, 21);
            _logFileFormatComboBox.TabIndex = 12;
            _logFileFormatComboBox.SelectedIndexChanged += OnLogFileFormatComboBoxSelectedIndexChanged;
            // 
            // enableLoggingCheckBox
            // 
            _enableLoggingCheckBox.AutoSize = true;
            _enableLoggingCheckBox.Location = new System.Drawing.Point(9, 20);
            _enableLoggingCheckBox.Name = "_enableLoggingCheckBox";
            _enableLoggingCheckBox.Size = new System.Drawing.Size(100, 17);
            _enableLoggingCheckBox.TabIndex = 0;
            _enableLoggingCheckBox.Text = Resources.LoggingUiPageEnableLoggingChexBox;
            _enableLoggingCheckBox.UseVisualStyleBackColor = true;
            _enableLoggingCheckBox.CheckedChanged += OnEnableLoggingCheckBoxCheckedChanged;
            // 
            // SiteLogging
            // 
            AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new System.Drawing.Size(459, 450);
            Controls.Add(_siteLoggingSettingsGroupBox);
            Name = "SiteLogging";
            _siteLoggingSettingsGroupBox.ResumeLayout(false);
            _siteLoggingSettingsGroupBox.PerformLayout();
            _pnlLogFileFormat.ResumeLayout(false);
            _pnlLogFileFormat.PerformLayout();
            _logScheduleGroupBox.ResumeLayout(false);
            _logScheduleGroupBox.PerformLayout();
            ((ISupportInitialize)(_fileSizeNumericUpDown)).EndInit();
            ResumeLayout(false);
        }
        #endregion

        /************************************************************************
           EVENTS                                                             
         ************************************************************************/
        private void OnBrowseDirectoryButtonClick(object sender, EventArgs e)
        {
            _folderBrowserDialog1.ShowNewFolderButton = true;
            _folderBrowserDialog1.Description = Resources.LoggingLogFileFolder;
            _folderBrowserDialog1.SelectedPath = _logFileDirectoryTextBox.Text;
            DialogResult dr = _folderBrowserDialog1.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                _logFileDirectoryTextBox.Text = _folderBrowserDialog1.SelectedPath;
            }
        }
        private void OnLogFileDirectoryTextBoxTextChanged(object sender, EventArgs e)
        {
            UpdateState(true);
        }
        private void OnRolloverCheckBoxCheckedChanged(object sender, EventArgs e)
        {
            UpdateState(true);
        }
        private void OnFileSizeNumericUpDownVisibleChanged(object sender, EventArgs e)
        {
            UpdateState(true);
        }
        private void OnHourlyRadioButtonCheckedChanged(object sender, EventArgs e)
        {
            UpdateState(true);
        }
        private void OnDailyRadioButtonCheckedChanged(object sender, EventArgs e)
        {
            UpdateState(true);
        }
        private void OnWeeklyRadioButtonCheckedChanged(object sender, EventArgs e)
        {
            UpdateState(true);
        }
        private void OnMonthlyRadioButtonCheckedChanged(object sender, EventArgs e)
        {
            UpdateState(true);
        }
        private void OnFileSizeRadioButtonCheckedChanged(object sender, EventArgs e)
        {
            _fileSizeNumericUpDown.Enabled = _fileSizeRadioButton.Checked;
            UpdateState(true);
        }
        private void OnLogFileFormatComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateState(true);
        }
        private void OnEnableLoggingCheckBoxCheckedChanged(object sender, EventArgs e)
        {
            _pnlLogFileFormat.Enabled = _enableLoggingCheckBox.Checked;
            UpdateState(true);
        }


        /************************************************************************
           Internal Classes                                                             
         ************************************************************************/
        /// <summary>
        /// Internal class used to create the page task list 
        /// </summary>
        private sealed class PageTaskList : TaskList
        {
            readonly CentralLoggingPage _owner;

            public PageTaskList(CentralLoggingPage owner)
            {
                _owner = owner;
            }

            public override ICollection GetTaskItems()
            {
                var list = new ArrayList();

                if (_owner._loggingMode != LoggingMode.Binary)
                {
                    Boolean enabled = (_owner._logFileFormatComboBox.SelectedIndex == 2 ||
                                        _owner._logFileFormatComboBox.SelectedIndex == 3);
                    var advancedTask = new MethodTaskItem(@"ShowAdvanced", "Advanced configuration", "Actions")
                                                      {Enabled = enabled};
                    list.Add(advancedTask);
                }
                return list.ToArray(typeof(TaskItem));
            }

// ReSharper disable UnusedMember.Local
            public void ShowAdvanced()
// ReSharper restore UnusedMember.Local
            {
                _owner.ShowSiteFlags();
            }
        }
    }
}
